home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / make / icmake-6.000 / icmake-6 / icmake / exec / funchdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-08  |  1.9 KB  |  66 lines

  1. /*
  2. \funcref{fun\_chdir}{void fun\_chdir ()}
  3.     {}
  4.     {}
  5.     {}
  6.     {}
  7.     {funchdir.c}
  8.     {
  9.  
  10.         This function expects a string to {\em chdir} to as second-but-last 
  11.         argument on the stack.  The last argument is the mode, {\em 
  12.         P\_CHECK} or {\em P\_NOCHECK}.  If the directory name is a non-null 
  13.         string, then the current working directory is set to the indicated 
  14.         path.  If the requested directory is an empty string, then a 
  15.         change-dir is performed to the startup directory.  
  16.         
  17.         Modifier {\em P\_CHECK} causes this function to abort upon failure.
  18.  
  19.         Return register {\em reg} is set to type {\em e\_str}. The value of 
  20.         the return register is set to the obtained working directory.  This 
  21.         may not be the requested directory if the {\em chdir} fails.  
  22.  
  23.     }
  24. */
  25.  
  26. #include "icm-exec.h"
  27.  
  28. void fun_chdir ()
  29. {
  30.     register char
  31.         *dir,
  32.         *last;
  33.     register int
  34.         mode;                        /* mode of operation */
  35.     char
  36.         newdir [_MAX_PATH];
  37.     static char
  38.         dirsep [2] = { DIRSEP, '\0' };
  39.         
  40.     mode = stack [sp].vu.intval;            /* get mode */
  41.     dir  = xstrdup (stack [sp - 1].vu.i->ls.str);    /* duplicate dest */
  42.  
  43.     if (! *dir)                        /* empty dest: */
  44.         dir = orgdir;                    /* change to org dir */
  45.  
  46.     last = dir + strlen (dir) - 1;            /* remove dir */
  47.     if ( last != dir &&                    /* separator */
  48.          (*last == '\\' || *last == '/') &&
  49.          *(last - 1) != ':'
  50.        )
  51.         *last = '\0';
  52.  
  53.     if (chdir (dir) && P_CHECKMODE (mode))        /* go to dir */
  54.         error ("chdir - can't change dir to %s", dir);    /* or quit if */
  55.                                 /* P_CHECK is on */
  56.     
  57.     xrealloc (dir, 0);                    /* free duplicate */
  58.     
  59.     getcwd (newdir, _MAX_PATH);                /* return value: */
  60.     if (newdir [strlen (newdir) - 1] != DIRSEP)        /* cwd */
  61.         strcat (newdir, dirsep);
  62.  
  63.     reg = newvar (e_str);
  64.     reg.vu.i->ls.str = xstrdup (newdir);
  65. }
  66.